﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;

namespace Base.Schema
{
    public class SchemaItem
    {
        public SchemaItem Parent { get; set; }

        // Attributes
        public string ElementName
        {
            get
            {
                // Do we have an ElementName attribute?
                SchemaElementNameAttribute[] names = (SchemaElementNameAttribute[])
                    this.GetType().GetCustomAttributes(typeof(SchemaElementNameAttribute), true);

                // No attributes :(
                if (names.Length == 0)
                {
                    return this.GetType().Name.ToLower();
                }

                return names[0].Name;
            }
        }

        public Preference[] GetPreferences()
        {
            return this.GetPreferences(null);
        }

        public Preference[] GetPreferences(string name)
        {
            List<Preference> prefs = new List<Preference>();

            // Enumerate on properties, save the ones that have Pref attributes
            PropertyInfo[] props = this.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                // Does this property have a Pref attribute defined?
                SchemaPreferenceAttribute[] attrs = (SchemaPreferenceAttribute[])
                    prop.GetCustomAttributes(typeof(SchemaPreferenceAttribute), false);

                // If not, move on
                if (attrs.Length == 0) continue;

                // Else, save the property
                foreach (SchemaPreferenceAttribute pref in attrs)
                {
                    if (name == null || pref.Name == name)
                    {
                        prefs.Add(new Preference(pref.Name, prop.GetValue(this, null)));
                        break;
                    }
                }
            }

            return prefs.ToArray();
        }

        public Dictionary<string, System.Collections.IEnumerable> GetCollections()
        {
            return this.GetCollections(null);
        }

        public Dictionary<string, System.Collections.IEnumerable> GetCollections(string name)
        {
            var cols = new Dictionary<string, System.Collections.IEnumerable>();

            // Enumerate on properties, save the ones that have Collection attribute
            PropertyInfo[] props = this.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                // Does this property have a Collection attribute defined?
                SchemaCollectionAttribute[] attrs = (SchemaCollectionAttribute[])
                    prop.GetCustomAttributes(typeof(SchemaCollectionAttribute), false);

                // If not, move on
                if (attrs.Length == 0) continue;

                // Else, save the property
                foreach (SchemaCollectionAttribute col in attrs)
                {
                    if (name == null || col.Name == name)
                    {
                        cols[col.Name] = (System.Collections.IEnumerable)prop.GetValue(this, null);
                        break;
                    }
                }
            }

            return cols;
        }
    }
}
